package com.guo.dms.common.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.guo.dms.common.menu.entity.Menu;
import com.guo.dms.common.menu.service.MenuService;
import com.guo.dms.common.menu.utils.MenuUtils;
import com.guo.dms.common.tree.entity.TreeNode;
import com.guo.dms.dept.user.utils.UserUtils;


@Controller
@RequestMapping("/")
public class LoginController {
	
	@Autowired
	MenuService menuService;
	
	@RequestMapping()
	public String index(HttpServletRequest  request){
		System.err.println("首页请求");
		if(UserUtils.isLogin()){
			
			this.setMenuTreeSession(request);
		    
			return "common/index";
		}else{
			return "redirect:/login";
		}
		
	}
	
	 
	
	
	/**
	 * 权限不通过时的请求
	 * @return
	 */
	@RequestMapping("unauthorized")
	public String unauthorized(){
		System.err.println("授权失败请求");
		return "common/unauthorized";
	}
	
	/**
	 * 登陆的get请求 用于显示页面
	 * @return
	 */
	@RequestMapping(value="login",method=RequestMethod.GET)
	public String login(){
		System.err.println("登陆页请求");
		
		return "common/login";
	}
	
	/**
	 * 登陆请求
	 * @param username
	 * @param password
	 * @param model
	 * @return
	 */
	@RequestMapping(value="login",method=RequestMethod.POST)
	public String loginForm(String username,String password,ModelMap model){
		System.err.println("登陆请求");
		Subject currentUser = SecurityUtils.getSubject();
		if (!currentUser.isAuthenticated()) { //是否认证通过
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		//token.setRememberMe(true);
		try {
			//认证成功
		currentUser.login(token);
		 
		System.err.println(currentUser.getPrincipal()+ " 身份认证成功 登陆成功！");
		return "redirect:/";
		
		}catch(UnknownAccountException un){
			String errmes = "登录失败：账户不存在！！";
			System.err.println( errmes);
			 model.addAttribute("error","nousername");
		} catch (AuthenticationException ae) {
			String errmes = "登录失败：认证不通过";
			System.err.println( errmes );
			model.addAttribute("username",username);
			model.addAttribute("error","nopassword");
		}
			return "common/login";
		}else{
			return "redirect:/";
		}
		 
	}
	
	/**
	 * 将menuTree加载到session中
	 * @param request
	 */
	private void setMenuTreeSession(HttpServletRequest  request){
		//加载并输出用户对应的menus
		if(request.getSession().getAttribute("menuTree")==null){
			 TreeNode<Menu> menuTree = menuService.getCurrentUserMenuTree(1);
			 MenuUtils.printMenuNode(menuTree, "-");
			 
			 request.getSession().setAttribute("menuTree", menuTree);
			 request.getSession().setAttribute("headerliveNode", menuTree.getChilds().get(0));
		}
	}
	
	/**
	 * 
	 * 退出登录时候清除缓存记录
	 * @return
	 */
	@RequestMapping(value="loginout")
	public String loginout(){
		SecurityUtils.getSubject().logout();
		return "common/login";
	}
}
